package controller;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bo.ReasonEstimateBo;
import bo.StacTraceBo;
import vo.CauseVo;
import vo.StackTraceVo;
import dao.hibernate.CauseVoDAO;
import dao.hibernate.StackTraceVoDAO;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.hsqldb.lib.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StackTraceEventStatServletV2 extends HttpServlet {
	private static Logger LOGGER = LoggerFactory.getLogger(StackTraceEventStatServletV2.class);
	private static ThreadLocal<StacTraceBo> STACKTRACEBO = new ThreadLocal<StacTraceBo>(){

		@Override
		protected StacTraceBo initialValue() {
			// TODO Auto-generated method stub
			return new StacTraceBo();
		}
		
	};
	/**
	 * Constructor of the object.
	 */
	public StackTraceEventStatServletV2() {
		super();
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		LOGGER.debug("doGet"  );
		String[] keyCauseUids = request.getParameterValues("keyCauseUid"); 
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		LOGGER.debug("keyCauseUid: {}",keyCauseUids  );
		RequestDispatcher dispatcher = request.getRequestDispatcher("ipDisplay.jsp");
	    dispatcher.forward(request, response); 
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(final HttpServletRequest request, final HttpServletResponse response)
			throws ServletException, IOException {
		LOGGER.debug("doPost"  );
		final String[] keyCauseUids = request.getParameterValues("keyCauseUid"); 
		final String startDate = request.getParameter("startDate");
		final String endDate = request.getParameter("endDate");
		LOGGER.debug("keyCauseUid: {}",keyCauseUids  );
		final StacTraceBo bo = STACKTRACEBO.get();
		Map<String,Map<String, Integer>>  keyCauseUidStackTraceStatMap = new ConcurrentHashMap<String,Map<String, Integer>>();
		if(ArrayUtils.isNotEmpty(keyCauseUids)){
			for(String keyCauseUid : keyCauseUids){
				final	Map<String, Integer> map = bo.getStaticsData(NumberUtils.toInt(keyCauseUid),startDate,endDate);
				keyCauseUidStackTraceStatMap.put(keyCauseUid, map);
				
			}
		}
		
		request.setAttribute("keyCauseUidStackTraceStatMap", keyCauseUidStackTraceStatMap);
		request.setAttribute("startDate", startDate);
		request.setAttribute("endDate", endDate);
		RequestDispatcher dispatcher = request.getRequestDispatcher("ipDisplayV2.jsp");
	    dispatcher.forward(request, response); 
		
	}

}
